package com.System.servlet;

import com.System.pojo.*;
import com.System.service.Comment;
import com.System.service.CommentImpl.CommentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/Comment/*")
public class CommentServlet extends BaseServlet{
    Comment service=new CommentService();
    public void selectAllComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");

        com.System.pojo.Comment target=new com.System.pojo.Comment();
        target.setBarName(barName);
        target.setUserName(username);
        target.setPostId(Integer.parseInt(postId));
        List<com.System.pojo.Comment> comments = service.selectAllComment(target);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (com.System.pojo.Comment comment:comments) {
            String createdTime = dateFormat.format(comment.getCreated_at());
            comment.setCreated_at(null);
            JSONObject commentObject = (JSONObject) JSON.toJSON(comment);
            commentObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = commentObject.toJSONString();
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void goBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String ref="http://localhost:8080/BarSystem/Bar.html?username="+username+"&barName="+barName+"";
        String jsonString = JSON.toJSONString(ref);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void selectOutOfMyComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");

        com.System.pojo.Comment target=new com.System.pojo.Comment();
        target.setBarName(barName);
        target.setUserName(username);
        target.setPostId(Integer.parseInt(postId));
        List<com.System.pojo.Comment> comments = service.selectOutOfMyComment(target);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (com.System.pojo.Comment comment:comments) {
            String createdTime = dateFormat.format(comment.getCreated_at());
            comment.setCreated_at(null);
            JSONObject commentObject = (JSONObject) JSON.toJSON(comment);
            commentObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = commentObject.toJSONString();
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void addComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        int id=Integer.parseInt(postId);
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Comment comment = JSON.parseObject(param, com.System.pojo.Comment.class);
        comment.setPostId(id);
        comment.setBarName(barName);
        comment.setUserName(username);
        service.addComment(comment);
        response.getWriter().write("success");
    }
    public void supportComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        String commentId = request.getParameter("commentId");
        int id=Integer.parseInt(commentId);

        Support target=null;
        target=new Support(id,username,barName);
        Support result=null;
        result=service.hasSupport(target);
        //查找有没有点赞过
        if(result==null){
            //查找有没有给过差评
            DisSupport res = service.hasDisSupport(new DisSupport(username, barName, id));
            if(res!=null){
                service.deleteDisFavor(res);
            }
            System.out.println(commentId);
            //更新post的点赞数进行+1的操作
            com.System.pojo.Comment temp = service.selectLikeNumbers(new com.System.pojo.Comment(Integer.parseInt(commentId)));
            temp.setLikeNumber(temp.getLikeNumber()+1);

            service.updateLikeNumber(temp);
            //添加点赞数据
            service.addSupport(target);
            response.getWriter().write("success");
        }else{
            response.getWriter().write("hasAdd");
        }
    }
    public void deleteMyComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String postId = request.getParameter("postId");
        System.out.println(postId);
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Comment comment = JSON.parseObject(param, com.System.pojo.Comment.class);
        String commentOwnerName = comment.getUserName();
        Post result = service.selectPostOwner(new Post(Integer.parseInt(postId)));
        if(commentOwnerName.equals(username)||username.equals(result.getName()))
        {
        comment.setBarName(barName);
        service.deleteMyComment(comment);
        response.getWriter().write("success");
        }else{
            response.getWriter().write("You have no ability");
        }
    }
    public void addDisSupport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String barName = request.getParameter("barName");
        String username = request.getParameter("username");
        String commentId = request.getParameter("commentId");
        int id=Integer.parseInt(commentId);

        DisSupport target=null;
        target=new DisSupport(username,barName,id);
        DisSupport result=null;
        result=service.hasDisSupport(target);
        //如果之前没有过差评记录就走以下程序
        if(result==null){
            Support res = service.hasSupport(new Support(id,username,barName));
            if(res!=null){
                //修改点赞数据
                com.System.pojo.Comment comment = service.selectLikeNumbers(new com.System.pojo.Comment(Integer.parseInt(commentId)));
                int ans = comment.getLikeNumber() - 1;
                comment.setLikeNumber(ans);
                service.updateLikeNumber(comment);
                service.deleteSupport(res);
            }
            service.addDisSupport(target);
            response.getWriter().write("success");
        }else{
            response.getWriter().write("hasAdd");
        }
    }
    public void updateComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        com.System.pojo.Comment comment = JSON.parseObject(param, com.System.pojo.Comment.class);
        service.updateComment(comment);
        response.getWriter().write("success");
    }







    }
